package de.thw.web.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import de.thw.web.dao.AbstractDAO;
import de.thw.web.model.AbstractModel;

public abstract class AbstractDAOImpl<T extends AbstractModel> implements
		AbstractDAO<T> {

	private SessionFactory sessionFactory;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@SuppressWarnings("unchecked")
	protected T load(Class<T> pClass, String uid) {
		return (T) sessionFactory.getCurrentSession().load(pClass, uid);
	};

	@Transactional(readOnly = false)
	public void save(T object) {
		this.sessionFactory.getCurrentSession().saveOrUpdate(object);
	};

	@SuppressWarnings("unchecked")
	public List<T> getAll(String table) {
		return this.sessionFactory.getCurrentSession()
				.createQuery("from " + table).list();
	}

};